
/**
 * 给定url地址自动下载
 * 作者：李广彬
 */
jQuery
		.extend({
			handleError : function(s, xhr, status, e) {
				// If a local callback was specified, fire it
				if (s.error) {
					s.error.call(s.context || s, xhr, status, e);
				}

				// Fire the global callback
				if (s.global) {
					(s.context ? jQuery(s.context) : jQuery.event).trigger(
							"ajaxError", [ xhr, s, e ]);
				}
			},
			createDownloadIframe : function(frameId, uri) {
				jQuery("<iframe>",{
					"src":uri,
					"id":frameId,
					"name":frameId,
					"style":"position:absolute;top:-9999px;left:-9999px"
				}).appendTo(document.body);
				return jQuery('#' + frameId).get(0);
			},
			ajaxFileDownload : function(s) {
				s = jQuery.extend({}, jQuery.ajaxSettings, s);
				var id = new Date().getTime();
				var frameId = 'jDownloadFrame' + id;
				var io = jQuery.createDownloadIframe(frameId, s.url);
				
				var xml = {};
				var downloadCallback = function(isTimeout) {
					var io = document.getElementById(frameId);
					try {
						if (io.contentDocument) {//非IE
							xml.responseText = io.contentDocument.body ? io.contentDocument.body.innerHTML:null;
							xml.responseXML = io.contentDocument.XMLDocument ? io.contentDocument.XMLDocument:io.contentDocument;
						} else if (io.contentWindow) {//IE
							xml.responseText = io.contentWindow.document.body ? io.contentWindow.document.body.innerHTML: null;
							xml.responseXML = io.contentWindow.document.XMLDocument ? io.contentWindow.document.XMLDocument:io.contentWindow.document;
						}
					} catch (e) {
						jQuery.handleError(s, xml, null, e);
					}
					if (xml || isTimeout == "timeout") {
						var status;
						try {
							status = isTimeout != "timeout" ? "success"	: "error";
							if (status != "error") {
								var data = jQuery.downloadHttpData(xml,s.dataType);
								if (s.success)
									s.success(data, status);
								// Fire the global callback
								if (s.global)
									jQuery.event.trigger("ajaxSuccess", [ xml,s ]);
							} else {
								jQuery.handleError(s, xml, status);
							}
						} catch (e) {
							status = "error";
							jQuery.handleError(s, xml, status, e);
						}

						// The request was completed
						if (s.global)
							jQuery.event.trigger("ajaxComplete", [ xml, s ]);

						// Handle the global AJAX counter
						if (s.global && !--jQuery.active)
							jQuery.event.trigger("ajaxStop");

						// Process result
						if (s.complete)
							s.complete(xml, status);

						jQuery(io).unbind();

						setTimeout(function() {
							try {
								jQuery(io).remove();
							} catch (e) {
								jQuery.handleError(s, xml, null, e);
							}
						}, 100);
						
						xml = null;
					}
				};

				jQuery('#' + frameId).load(downloadCallback);
				return {
					abort : function() {
					}
				};

			},

			downloadHttpData : function(r, type) {
				var data = !type;
				data = type == "xml" || data ? r.responseXML : r.responseText;
				// If the type is "script", eval it in global context
				if (type == "script")
					jQuery.globalEval(data);
				// Get the JavaScript object, if JSON is used.
				if (type == "json")
					data = r.responseText;
					var start = data.indexOf(">");
					if (start != -1) {
						var end = data.indexOf("<", start + 1);
						if (end != -1) {
							data = data.substring(start + 1, end);
						}
					}
					if(data){
						eval("data = " + data);
					}
					
				// evaluate scripts within html
				if (type == "html")
					jQuery("<div>").html(data).evalScripts();

				return data;
			}
		});
